//
//  LoginViewController.m
//  13-私人通讯录
//
//  Created by wangneihan on 15/12/25.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#define rememberUserName @"rememberUserName"
#define autoLogin @"autoLogin"
#define userText @"userName"
#define pwdText @"pwd"

#import "LoginViewController.h"
#import "MBProgressHUD+CZ.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTxt;

@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;

@property (weak, nonatomic) IBOutlet UISwitch *remeberCon;

@property (weak, nonatomic) IBOutlet UISwitch *autoLoginCon;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

- (IBAction)loginBtnClick;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1.订阅文本框 textChange 事件
    [self.userNameTxt addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [self.pwdTxt addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [self textChange];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.remeberCon.on = [defaults boolForKey:rememberUserName];
    self.autoLoginCon.on = [defaults boolForKey:autoLogin];
    
    self.userNameTxt.text = [defaults objectForKey:userText];
    if(self.remeberCon.isOn)
    {
        self.pwdTxt.text = [defaults objectForKey:pwdText];
    }
    
    [self textChange];
    if(self.autoLoginCon.isOn)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             [self loginBtnClick];
        });
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 1. 监听文本框改变事件，控制登陆按钮
 2. 使用偏好设置记录 记住密码、自动登陆
 3. 点击登陆，判断中账号密码
 */

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)loginBtnClick {
    NSString *userName = self.userNameTxt.text;
    NSString *pwd = self.pwdTxt.text;
    [MBProgressHUD showMessage:@"登录中。。。。"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUD];
        if([userName isEqualToString:@"wxd"] && [pwd isEqualToString:@"123"])
        {
            [self performSegueWithIdentifier:@"toContactView" sender:nil];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:userName forKey:userText];
            if(self.remeberCon.isOn)
            {
                [defaults setObject:pwd forKey:pwdText];
            }
            [defaults synchronize];
            
        }
        else
        {
            [MBProgressHUD showError:@"账号或密码错误！"];
        }
    });
    
}

-(void)textChange
{
    self.loginBtn.enabled = (self.userNameTxt.text.length!=0 && self.pwdTxt.text.length!=0);
}
@end
